package controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Controller
public class TestController02 {
	@RequestMapping(value="/showImg")
	public void showImg(OutputStream out)throws Exception{
		InputStream in = new FileInputStream("D:/pic.jpg");
		byte[] buff = new byte[128];
		int len = 0;
		while((len=in.read(buff))!=-1){
			out.write(buff,0,len);
		}
		out.close();
		in.close();
	}
	@ResponseBody
	@RequestMapping(value="/down")
	public byte[] download(HttpServletResponse resp)throws Exception{
		resp.setHeader("Content-Disposition", "attachment;filename=xx.jpg");
		InputStream in = new FileInputStream("D:/pic.jpg");
		byte[] buff = new byte[in.available()];
		in.read(buff);
		in.close();
		return buff;
	}
	
	
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String upload(String uname,MultipartFile img)throws Exception{
		System.out.println(uname);
		System.out.println(img.getOriginalFilename());
		System.out.println(img.getInputStream().available());
		return "suc";
	}
}
